package com.air.controller;

import com.air.entity.Employee;
import com.air.service.IDeptService;
import com.air.service.IEmployeeService;
import com.air.util.PageResult;
import com.air.util.Result;
import com.air.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;


    @RequestMapping("/list")
    public Result getStudent() {
        return Result.ok().data("list", employeeService.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public Result findByPage(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
        PageResult<Employee> pr = employeeService.findByPage(page, pageSize);
        return Result.ok().data("pr", pr);
    }

    @PostMapping("/search/{page}/{pageSize}")
    public Result search(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize,
                         @RequestBody(required = false) EmployeeVo employeeVo) {
        PageResult<Employee> pr = employeeService.search(page, pageSize, employeeVo);
        return Result.ok().data("pr", pr);
    }

    @PostMapping("/save")
    public Result add(@RequestBody Employee employee) {
        employee.setDeleted(0);
        return employeeService.save(employee) ? Result.ok().message("添加成功喵!") : Result.error().message("添加失败，嘻嘻！");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Employee employee) {
        employee.setDeleted(0);
        return employeeService.updateById(employee) ? Result.ok().message("修改成功！") : Result.ok().message("修改失败！");
    }

    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        return employeeService.removeBatchByIds(ids) ? Result.ok().message("删除成功！") : Result.ok().message("删除失败！");
    }

}
